/**
 * loading组件
 */
import { Loading } from 'element-ui';

let loadingCount = 0;
let loading;
let loadingTimer = null // 延时loading定时器

const startLoading = () => {
    loading = Loading.service({
        lock: true,
        text: '拼命加载中...',//可以自定义文字
        spinner: 'el-icon-loading',//自定义加载图标类名
        background: 'rgba(0, 0, 0, 0.7)'//遮罩层背景色
    });
};

const endLoading = () => {
    loading && loading.close();
};

export const showLoading = () => {
    if (loadingCount === 0) {
        loadingTimer = setTimeout(() => {
            startLoading();
        }, 500)
    }
    loadingCount += 1;
};

export const hideLoading = () => {
    if (loadingCount <= 0) {
        return;
    }
    loadingCount -= 1;
    if (loadingCount === 0) {
        loadingTimer && clearTimeout(loadingTimer)
        endLoading();
    }
};